using System.Collections.Generic;
using Light.Assets.Scripts.EventManager;
using UnityEngine;

namespace Light
{
    /// <summary>
    ///     ���ƶ�Ӧ���ֵ�UI����ӦEvent
    /// </summary>
    public class QuickSlotBarController_Event : MonoBehaviour
    {
        private BasePanel _panel;

        private BasePanel panel
        {
            get
            {
                if (_panel == null)
                    _panel = UIManager.Instance.GetRegisterPanel<QuickSlotBarController>("QuickSlotBarController");
                return _panel;
            }
            set => _panel = value;
        }

        private void Awake()
        {
            EventManager.Register<QuickSlotPanelInitEvent>(OnQuickSlotPanelInitEventHandle);
            EventManager.Register<QuickUpdateItemJsonIndexEvent>(OnQuickUpdateItemJsonIndexEventHandle);

            EventManager.Register<ItemEquipEvent>(OnItemEquipEventHandle);
            EventManager.Register<ItemRelieveEvent>(OnItemRelieveHandle);
        }


        /// <summary>
        ///     ����������Ƶ�UI��index��Ӱ��
        /// </summary>
        /// <param name="event"></param>
        private void OnQuickUpdateItemJsonIndexEventHandle(QuickUpdateItemJsonIndexEvent @event)
        {
            var res = new GoodsJsonData();
            foreach (var item in ItemManager.GetItemsEquipJsonDataAsync())
                if (@event.item.id == item.id.ToString())
                {
                    res = item;
                    res.index = @event.index;
                }

            //�����һʱ���ҵ��ˣ��Ͳ�ȥ�������
            if (res != null)
            {
                ItemManager.DeleteItem(res.id);
                ItemManager.AddNewJsonDataToEquipData(res);
                return;
            }

            foreach (var item in ItemManager.GetItemsJsonDataAsync())
                if (@event.item.id == item.id.ToString())
                {
                    res = item;
                    res.index = @event.index;
                }

            ItemManager.AddNewJsonDataToData(res);

            //@event.Dispose();
        }

        private void OnItemRelieveHandle(ItemRelieveEvent @event)
        {
            using var info = QuickSlotBarInfo.Get();
            info.type = FunctionBarOperationType.Delete;
            info.Items = new List<(int, AbstractItemSO)> { (0, @event.item) };
            //UIManager.Instance.GetRegisterPanel<QuickSlotBarController>("QuickSlotBarController").Refresh(info);

            RequestRefreshPanel(info);


            //@event.Dispose();
        }

        private void OnItemEquipEventHandle(ItemEquipEvent @event)
        {
            var info = QuickSlotBarInfo.Get();
            info.type = FunctionBarOperationType.Add;
            info.Items = new List<(int, AbstractItemSO)>();
            info.Items.Add((0, @event.item)); //��������ʹ�õĲ��Ǵ�0��ʼ�ģ���������0ָ���ǻ�δ����
            //Ϊ�Ѿ�ת�Ƶ����ݷ���index
            //BasePanel panel = UIManager.Instance.GetRegisterPanel<QuickSlotBarController>("QuickSlotBarController");
            //panel.Refresh(info);
            RequestRefreshPanel(info);

            //@event.Dispose();
        }

        private async void OnQuickSlotPanelInitEventHandle(QuickSlotPanelInitEvent @event)
        {
            List<(int, AbstractItemSO)> res = new();

            foreach (var item in await ItemManager.GetGoodItemsEquipDataAsync())
            foreach (var json in ItemManager.GetItemsEquipJsonDataAsync())
                if (item.id == json.id.ToString())
                    res.Add((json.index, item));

            //@event.Dispose();

            using var evt = QuickSlotBarInfo.Get();
            evt.type = FunctionBarOperationType.Init;
            evt.Items = res;
            RequestRefreshPanel(evt);
        }

        private void RequestRefreshPanel(QuickSlotBarInfo info)
        {
            panel.Refresh(info);
        }
    }
}